package com.bw.service.impl;

import com.bw.domain.Role;
import com.bw.domain.User;
import com.bw.exception.MyRuntimeException;
import com.bw.mapper.UserMapper;
import com.bw.service.RoleService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhaijiaqi
 * @Date: 2025/3/12 21:01
 * @Description:
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Transactional(rollbackFor = MyRuntimeException.class)

    @Override
    public boolean add(Role role) {

        try {
            if(role.getId()==null){
                userMapper.addrole(role);
            }else{
                userMapper.deleteByroleid(role.getId());
            }


            if(!StringUtil.isEmpty(role.getIds())){
                String ids = role.getIds();
                String[] split = ids.split(",");
                for (int i = 0; i < split.length; i++) {
                    userMapper.insertroleMenu(role.getId(),split[i]);
                }


            }
            return true;
        }catch (MyRuntimeException e){
            e.printStackTrace();
            throw new MyRuntimeException(e.getMessage());
        }





    }

    @Override
    public Role getrole(String roleName) {
        return userMapper.getrole(roleName);
    }

    @Override
    public List<Integer> getmenuByroleId(Integer id) {
        return userMapper.getmenuByroleId(id);
    }
}
